# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2021 Iñigo Martinez <inigomartinez@gmail.com>

generated_inc = include_directories('.')

gen_sources = []
gen_headers = []
gen_sections = []
gen_sections_deps = []

# Error types
enum_types = 'mbim-error-types'

gen_headers += custom_target(
  enum_types + '.h',
  input: mbim_errors_header,
  output: enum_types + '.h',
  command: [
    python,
    mbim_mkenums,
    '--fhead', '#ifndef __LIBMBIM_GLIB_ERROR_TYPES_H__\n#define __LIBMBIM_GLIB_ERROR_TYPES_H__\n#include "mbim-errors.h"\n',
    '--template', files(templates_dir / enum_types + '.h.template'),
    '--ftail', '#endif /* __LIBMBIM_GLIB_ERROR_TYPES_H__ */\n',
    '@INPUT@'],
  capture: true,
  install: true,
  install_dir: mbim_glib_pkgincludedir,
)

gen_sources += custom_target(
  enum_types + '.c',
  input: mbim_errors_header,
  output: enum_types + '.c',
  command: [
    python,
    mbim_mkenums,
    '--fhead', '#include "mbim-errors.h"\n#include "mbim-error-types.h"\n',
    '--template', files(templates_dir / enum_types + '.c.template'),
    '@INPUT@'],
  capture: true,
)

enum_types = 'mbim-error-quarks'

gen_sources += custom_target(
  enum_types + '.c',
  input: mbim_errors_header,
  output: enum_types + '.c',
  command: [
    python,
    mbim_mkenums,
    '--fhead', '#include "mbim-errors.h"\n#include "mbim-error-types.h"\n',
    '--template', files(templates_dir / enum_types + '.c.template'),
    '@INPUT@'],
  capture: true,
)

# Enum types
enum_types = 'mbim-enum-types'

gen_headers += custom_target(
  enum_types + '.h',
  input: mbim_enums_headers,
  output: enum_types + '.h',
  command: [
    python,
    mbim_mkenums,
    '--enums-only',
    '--fhead', '#ifndef __LIBMBIM_GLIB_ENUM_TYPES_H__\n#define __LIBMBIM_GLIB_ENUM_TYPES_H__\n#include "mbim-uuid.h"\n#include "mbim-cid.h"\n#include "mbim-message.h"\n#include "mbim-enums.h"\n#include "mbim-tlv.h"\n',
    '--template', files(templates_dir / enum_types + '.h.template'),
    '--ftail', '#endif /* __LIBMBIM_GLIB_ENUM_TYPES_H__ */\n',
    '@INPUT@'],
  capture: true,
  install: true,
  install_dir: mbim_glib_pkgincludedir,
)

gen_sources += custom_target(
  enum_types + '.c',
  input: mbim_enums_headers,
  output: enum_types + '.c',
  command: [
    python,
    mbim_mkenums,
    '--enums-only',
    '--fhead', '#include "mbim-enum-types.h"\n',
    '--template', files(templates_dir / enum_types + '.c.template'),
    '@INPUT@'],
  capture: true,
)

# Flag types
enum_types = 'mbim-flag-types'

gen_headers += custom_target(
  enum_types + '.h',
  input: mbim_enums_headers,
  output: enum_types + '.h',
  command: [
    python,
    mbim_mkenums,
    '--flags-only',
    '--fhead', '#ifndef __LIBMBIM_GLIB_FLAG_TYPES_H__\n#define __LIBMBIM_GLIB_FLAG_TYPES_H__\n#include "mbim-uuid.h"\n#include "mbim-cid.h"\n#include "mbim-message.h"\n#include "mbim-enums.h"\n#include "mbim-tlv.h"\n',
    '--template', files(templates_dir / enum_types + '.h.template'),
    '--ftail', '#endif /* __LIBMBIM_GLIB_FLAG_TYPES_H__ */\n',
    '@INPUT@'],
  capture: true,
  install: true,
  install_dir: mbim_glib_pkgincludedir,
)

gen_sources += custom_target(
  enum_types + '.c',
  input: mbim_enums_headers,
  output: enum_types + '.c',
  command: [
    python,
    mbim_mkenums,
    '--flags-only',
    '--fhead', '#include "mbim-flag-types.h"\n',
    '--template', files(templates_dir / enum_types + '.c.template'),
    '@INPUT@'],
  capture: true,
)

services_data = [
  ['atds'],
  ['auth'],
  ['basic-connect', 'ms-basic-connect-v2', 'ms-basic-connect-v3'],
  ['dss'],
  ['intel-firmware-update', 'intel-firmware-update-v2'],
  ['intel-thermal-rf'],
  ['ms-basic-connect-extensions', 'ms-basic-connect-extensions-v2', 'ms-basic-connect-extensions-v3'],
  ['ms-uicc-low-level-access'],
  ['ms-firmware-id'],
  ['ms-host-shutdown'],
  ['ms-sar'],
  ['ms-voice-extensions'],
  ['phonebook'],
  ['proxy-control'],
  ['qdu'],
  ['quectel'],
  ['qmi'],
  ['sms'],
  ['stk'],
  ['ussd'],
  ['intel-mutual-authentication'],
  ['intel-tools'],
  ['google'],
  ['fibocom'],
  ['compal'],
  ['intel-at-tunnel'],
]

foreach service_data: services_data
  service = service_data[0]
  name = 'mbim-' + service

  input = []
  foreach service_file: service_data
    input += data_dir / 'mbim-service-@0@.json'.format(service_file)
  endforeach

  generated = custom_target(
    name,
    input: input,
    output: [name + '.c', name + '.h', name + '.sections'],
    command: [mbim_codegen, '--output', '@OUTDIR@' / name, '@INPUT@'],
    install: true,
    install_dir: [false, mbim_glib_pkgincludedir, false],
  )

  gen_sources += generated[0]
  gen_headers += generated[1]

  # FIXME: the third target generated by custom target can't by used because is not a known
  #        source file, to the path has to be used. the first workaround is to use the
  #        build paths to point to the files, and the second workaround is to use
  #        custom target objects to force its building.
  gen_sections += [meson.current_build_dir() / name + '.sections']
  gen_sections_deps += [generated]
endforeach

c_flags = [
  '-DLIBMBIM_GLIB_COMPILATION',
  '-DG_LOG_DOMAIN="Mbim"',
  '-Wno-unused-function',
]

libmbim_glib_generated = static_library(
  'mbim-glib-generated',
  sources: gen_sources + gen_headers,
  include_directories: libmbim_glib_inc,
  dependencies: glib_deps,
  c_args: c_flags,
)

generated_dep = declare_dependency(
  sources: gen_headers,
  include_directories: [libmbim_glib_inc, generated_inc],
  dependencies: glib_deps,
)
